//
//  BoZeCarOilDetailVC.swift
//  BoZeCalendar
//
//  Created by 李东 on 2024/4/12.
//

import UIKit

class BoZeCarOilDetailVC: BoZeBaseVC {
    
    let CellID = NSStringFromClass(BoZeDetailCell.self)
    
    
    var model = BoZeCarModel()
    var list = [BoZeOilModel]()
    
    init(model: BoZeCarModel = BoZeCarModel()) {
        super.init(nibName: nil, bundle: nil)
        self.model = model
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        hideNavgaitionBar()
        showBgImgView = true
        bgImgView.image = UIImage(named: "img_me_bg")
        showPopBtn = true
        navTitle = model.carName
        view.addSubview(addBtn)
        addBtn.snp.makeConstraints { make in
            make.width.height.equalTo(44)
            make.centerY.equalTo(popBtn)
            make.right.equalTo(-10)
        }
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(CGFloat.navHeight + 20)
        }
        

        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        list = BoZeDataHelper.shared.getLocalList().first(where: {$0.ids == model.ids})?.list ?? [BoZeOilModel]()
        tableView.reloadData()
        
        var allOil = 0, allCost = CGFloat(0), allConsumption = Double(0)
        model.list.forEach { mm in
            allOil += mm.addOil
            allCost += Double(mm.cost) ?? 0
        }
        if model.list.count >= 2 {
            let m1 = model.list[0]
            let m2 = model.list[1]
            allConsumption = CGFloat((m2.addOil - m1.remainOil) / ((m1.km - m2.km)/100))
        }
        headerView.configData(allOil: allOil, allCost: allCost, allConsumption: allConsumption)
    }
    
    @objc func addOil() {
        navigationController?.pushViewController(BoZeAddOilVC(model: model), animated: true)
    }
    
    lazy var tableView: UITableView = {
        let temp = UITableView(frame: .zero, style: .grouped)
        temp.separatorStyle = .none
        temp.backgroundColor = .clear
        temp.delegate = self
        temp.dataSource = self
        temp.rowHeight = 120
        temp.contentInsetAdjustmentBehavior = .never
        temp.tableHeaderView = headerView
        temp.tableFooterView = UIView(frame: .zero)
        temp.register(UINib(nibName: "BoZeDetailCell", bundle: nil), forCellReuseIdentifier: CellID)
        return temp
    }()
    
    lazy var addBtn: UIButton = {
        let temp = UIButton(type: .custom)
        temp.setImage(UIImage(named: "img_add_oil"), for: .normal)
        temp.addTarget(self, action: #selector(addOil), for: .touchUpInside)
        return temp
    }()

    lazy var headerView: BoZeDetailHeaderView = {
        let temp = BoZeDetailHeaderView(frame: CGRect(x: 0, y: 0, width: CGFloat.screenWidth, height: 110))
        return temp
    }()

}

extension BoZeCarOilDetailVC: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        list.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: CellID, for: indexPath) as! BoZeDetailCell
        cell.model = list[indexPath.section]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        section == 0 ? 10 : 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        UIView()
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        10
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        UIView()
    }
}
